package com.imay.live.activity.family;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.R;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.activity.family.controller.FamilyAcceptController;
import com.imay.live.model.FamilyApplyUserInfo;
import com.imay.live.model.UserInfoModel;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.ToastUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.List;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * Created by Administrator on 2017/9/6 0006.
 */

public class FamilyAcceptActivity extends BaseRequestActivity implements View.OnClickListener {

    public static final String KEY_FAMILY_ID = "key_family_id";
    private FamilyAcceptController controller;

    private String requestAcceptUserCommand;
    private String requestPassUserCommand;
    private String requestRejectUserCommand;

    private int familyid;

    boolean isPullToRresh = false;

    /**
     * @param context
     */
    public static void startActivity(Context context, int familyid) {
        Intent intent = new Intent(context, FamilyAcceptActivity.class);
        intent.putExtra(KEY_FAMILY_ID, familyid);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_family_pass;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        familyid = getIntent().getIntExtra(KEY_FAMILY_ID, -1);
        setTitle(R.string.str_family_accept);
        getTitleBar().setRightImageVisibility(false);
        getTitleBar().setRightTextVisibility(true);
        getTitleBar().addRightTextView(getString(R.string.str_family_accept_all));
        getTitleBar().setRightTextOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                controller.selectAll();
            }
        });
        controller = (FamilyAcceptController) FamilyAcceptController.Builder.create(this, FamilyAcceptController.class).build();

        controller.initRresh(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                isPullToRresh = true;
                loadJoinApply();
            }
        });

        loadJoinApply();
    }

    private void loadJoinApply() {
        if (familyid == -1) {
            return;
        }
        requestAcceptUserCommand = ProtoRequestHelper.task(ProtoHttpFactory.getFamilyAcceptUsers(familyid)).setTag(this.hashCode()).request(this);
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        super.onRequestSuccess(command, data);
        if (command.equals(requestAcceptUserCommand)) {
            Result<JSONObject> resp = JSONObject.parseObject(data, new TypeReference<Result<JSONObject>>() {
            }.getType());
            if (!TextUtils.isEmpty(resp.data.getString("applyInfo"))) {
                List<FamilyApplyUserInfo> userInfos = JSONObject.parseObject(resp.data.getString("applyInfo"), new TypeReference<List<FamilyApplyUserInfo>>() {
                }.getType());
                controller.setUserData(userInfos);
                if (isPullToRresh) {
                    isPullToRresh = false;
                    controller.finishRresh();
                }
            }
        } else if (command.equals(requestPassUserCommand)) {
            Result<String> resp = JSONObject.parseObject(data, new TypeReference<Result<String>>() {
            }.getType());
            if (resp.result == CodeSuccess) {
                controller.passToast();
            }
        } else if (command.equals(requestRejectUserCommand)) {
            Result<String> resp = JSONObject.parseObject(data, new TypeReference<Result<String>>() {
            }.getType());
            if (resp.result == CodeSuccess) {
                controller.rejectToast();
            }
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        super.onRequestError(command, code, message);
        ErrorResult.toastError(this, code);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_family_pass:
                requestPassUserCommand = controller.passUsers();
                break;
            case R.id.tv_family_reject:
                requestRejectUserCommand = controller.rejectUsers();
                break;
        }
    }
}
